package student.stu23002123.chapter07;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.channels.ClosedSelectorException;

class A{}

class B{

    int age;
    String name;
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //必须要有无参构造方法才能通过newInstance构造对象
    //如果添加有参构造方法编译器则不会使用默认的无参构造方法
    public B(){
        System.out.println("B类被构造了");
    }

    @Override
    public String toString() {
        return "B{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

}

class C{
    //利用有参构造方法newInsteance创建对象
    int age;


    String name;
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public C(int age, String name) {
        this.age = age;
        this.name = name;
        System.out.println("调用了构造方法"+name+"/"+age);
    }
}
public class Reflex {
    public static void main(String[] args) {
        Class c1 = null;
        Class c2 = null;
        Class c3 = null;
        Class c4 = null;
        Class c5 = null;

        c1 = new A().getClass();
        c2 = A.class;
        try {
            c3 = Class.forName("student.stu23002123.chapter07.A");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }


        System.out.println(c1.getName());
        System.out.println(c2.getName());
        System.out.println(c3.getName());

        //class类实例化对象
        try {
            c4 = Class.forName("student.stu23002123.chapter07.B");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        B b = null;
        try {
            //newInstance创建对象
            b = (B) c4.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        b.setAge(22);
        b.setName("张子阳");
        System.out.println(b.toString());

        try {
            c5 = Class.forName("student.stu23002123.chapter07.C");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        C c = null;
        Constructor cons[] = c5.getConstructors();

        try {
            c = (C) cons[0].newInstance(22,"张子阳");
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        System.out.println(c);
    }
}
